class Solution {
public:
    vector<int> avoidFlood(vector<int>& rains) {
        
        vector<int> vecRet(rains.size(),1);
        unordered_map<int,int> has;
        set<int> st;
        int nCnt=0;
        for(int i=0;i<rains.size();++i){
            if(rains[i]==0){
                st.insert(i);
            }
            else{
                vecRet[i]=-1;
                if(has.count(rains[i])){
                    auto it=st.lower_bound(has[rains[i]]);
                    if(it==st.end()){
                        return {};
                    }
                    vecRet[*it]=rains[i];
                    st.erase(it);
                }
                has[rains[i]]=i;
            }
        }

        return vecRet;
    }
};
